package com.qiaofang.collectmanage.stub.constants;

import java.util.*;

/**
 * Copyright (C), 2002-2017, 上海巧房信息技术有限公司
 * FileName: BelongerEnum
 * @Author:   yi.li
 * Date:     2018/7/2 11:05
 * Description: 归属人
 */
public enum BelongerEnum {
    PERSONAL("个人","PERSONAL"),
    AGENT("经纪人", "AGENT");
    /**
     * 枚举值
     */
    private String value;
    /**
     * 描述
     */
    private String desc;

    BelongerEnum(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }
    /**
     * 判断是否存在枚举value
     */
    public static boolean valueExist(String value) {
        return Arrays.stream(BelongerEnum.values()).anyMatch(obj -> obj.getValue().equals(value));
    }

    /**
     * 判断是否存在枚举desc
     */
    public static boolean descExist(String desc) {
        return Arrays.stream(BelongerEnum.values())
                .anyMatch(obj -> obj.getDesc().equals(desc));
    }

    /**
     * 匹配获取对应的Desc
     */
    public static String getExistDesc(String value) {
        return Arrays.stream(BelongerEnum.values()).filter(obj -> obj.getValue().equals(value))
                .map(obj -> obj.getDesc()).findFirst().orElse(null);
    }


    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public static BelongerEnum getEnum(String value) {
        BelongerEnum resultEnum = null;
        BelongerEnum[] enumAry = BelongerEnum.values();
        for (int i = 0; i < enumAry.length; i++) {
            if (enumAry[i].getValue() == value) {
                resultEnum = enumAry[i];
                break;
            }
        }
        return resultEnum;
    }

    public static Map<String, Map<String, Object>> toMapMap() {
        BelongerEnum[] ary = BelongerEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<String, Map<String, Object>>();
        for (int num = 0; num < ary.length; num++) {
            Map<String, Object> map = new HashMap<String, Object>();
            String key = String.valueOf(getEnum(ary[num].getValue()));
            map.put("value", String.valueOf(ary[num].getValue()));
            map.put("desc", ary[num].getDesc());
            enumMap.put(key, map);
        }
        return enumMap;
    }

    public static Map<String, String> toMap() {
        BelongerEnum[] ary = BelongerEnum.values();
        Map<String,String> enumMap = new HashMap<>();
        for (int i = 0; i < ary.length; i++) {
            enumMap.put(ary[i].getDesc(), ary[i].getValue());
        }
        return enumMap;
    }

    public static List toList() {
        BelongerEnum[] ary = BelongerEnum.values();
        List list = new ArrayList();
        for (int i = 0; i < ary.length; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("value", String.valueOf(ary[i].getValue()));
            map.put("desc", ary[i].getDesc());
            list.add(map);
        }
        return list;
    }

    /**
     * 取枚举的json字符串
     *
     * @return
     */
    public static String getJsonStr() {
        BelongerEnum[] enums = BelongerEnum.values();
        StringBuilder jsonStr = new StringBuilder("[");
        for (BelongerEnum senum : enums) {
            if (!"[".equals(jsonStr.toString())) {
                jsonStr.append(",");
            }
            jsonStr.append("{id:'").append(senum.toString()).append("',desc:'").append(senum.getDesc()).append("',value:'").append(senum.getValue()).append("'}");
        }
        jsonStr.append("]");
        return jsonStr.toString();
    }


}
 